/*
 * Copyright 2008 by Robert Konigsberg
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the same terms as Perl.
 *
 * @see http://www.perl.com/perl/misc/Artistic.html
 */
package visitor.maze;

import java.util.HashSet;
import java.util.Set;

/**
 * Abstract implementation of {@link Visitor} that prevents rooms from
 * being visited more than once.
 *
 * <p>Subclasses that override {@link #visit(Room)} must call
 * {@code super.visit(room)} in order to register that a room has been visited.
 *
 * @author kberg
 */
public abstract class NonRepeatingVisitor implements Visitor {
  Set<Room> visited = new HashSet<Room>();

  /**
   * Returns {@code true} if this room has not yet been visited.
   */
  public boolean shouldVisit(Room room) {
    return !visited.contains(room);
  }

  /**
   * Registers that a room has been visited. It's expected that subclasses
   * that override this method must call this superclass implementation.
   */
  public void visit(Room room) {
    visited.add(room);
  }
}
